package repaso_bolita;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class ClaPanelBolita extends JPanel implements KeyListener, MouseListener {
	// private static final Color COLOR_BOLITA = new Color(255, 0, 0);
	private static final int NUM_BOLAS = 3;
	
	Bolita bolaRebota[] = new Bolita[NUM_BOLAS];

	public ClaPanelBolita() {
		for(int con=0; con <NUM_BOLAS; con++){			
			bolaRebota[con] = new Bolita(con+1,con+1, this);
		}		
		addKeyListener(this);
		setFocusable(true);
		this.addMouseListener(this);
	}
	
	
	
	private void moviminto() {
		for(int con=0; con <NUM_BOLAS; con++){
			bolaRebota[con].moviminto();
			
			for(int i=0 ;i<NUM_BOLAS; i++){
				if(bolaRebota[con].getRectBolita().intersects(bolaRebota[i].getRectBolita())){
					colision(bolaRebota[con],bolaRebota[i]);
										
				}
			}
			}
		}
	

	private void colision(Bolita bolita, Bolita bolita2) {
		bolita.iVelX=-bolita.iVelX;
		bolita.iVelY=-bolita.iVelY;
		bolita2.iVelX=-bolita2.iVelX;
		bolita2.iVelY=-bolita2.iVelY;
		
			
	}


	@Override
	public void paint(Graphics grafica) {
		super.paint(grafica);
		Graphics2D graf2D = (Graphics2D) grafica;
		setBackground(Color.cyan);
		for(int con=0; con <NUM_BOLAS; con++){
			bolaRebota[con].paint(graf2D, new Color(80*con+10, 0, 0));
		}
	}


	public static void main(String[] args) throws InterruptedException {
		JFrame frame = new JFrame("Rebote de Bolitas");
		ClaPanelBolita mesaJuego = new ClaPanelBolita();
		frame.add(mesaJuego);
		frame.setSize(300, 400);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		while (true) {
			mesaJuego.moviminto();
			mesaJuego.repaint();
			Thread.sleep(10);
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
		for(int con=0; con <NUM_BOLAS; con++){
			bolaRebota[con].keyPressed(e);
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}



	@Override
	public void mouseClicked(MouseEvent e) {
		
		for(int con=0; con <NUM_BOLAS; con++){
			if(e.getX()>=bolaRebota[con].iPosX && e.getX()<=bolaRebota[con].iPosX+bolaRebota[con].iDiametro && e.getY()>=bolaRebota[con].iPosY && e.getY()<=bolaRebota[con].iPosY+bolaRebota[con].iDiametro){				
					bolaRebota[con].redimensionar();				
			}
				
		}
		
	}



	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}